package edu.suda.LoopCodePlus.service.impl;

import edu.suda.LoopCodePlus.entity.Collections;
import edu.suda.LoopCodePlus.mapper.CollectionsMapper;
import edu.suda.LoopCodePlus.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionsMapper collectionsMapper;
    @Override
    public List<Collections> findCollectionById(Integer userId){
        try{
            return collectionsMapper.select_CollectionsByUserId(userId);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }
    @Override
    public boolean addCollection(Collections collections){
        try{
            Collections collections1 = collectionsMapper.select_CollectionsByColName(collections.getColName());
            if (collections1!=null){
                return false;
            }
        }catch (Exception e){
            return false;
        }
        try{
            collectionsMapper.insert_Collections(collections);
            System.out.println("添加成功");
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
    @Override
    public boolean updateCollection(Collections collections){
        try{
            collectionsMapper.update_CollectionsById(collections.getColId(),collections.getUserId(),collections);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    @Override
    public Collections findCollectionById(Integer userId, Integer colId){
        Collections collections=null;
        try{
            collections = collectionsMapper.select_CollectionsBylId(userId,colId);
            return collections;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return collections;
        }
    }
    @Override
    public Integer deleteCollectionById(Integer userId, Integer colId){
        try {
            Collections collection = collectionsMapper.select_CollectionsByColId(colId);
            if(collection==null) return 1; //数据集不存在
            if(!Objects.equals(collection.getUserId(), userId)) return 2;  //用户权限不足
        }catch (Exception e){
            return 3; //数据库查询失败
        }
        try{
            System.out.println(userId);
            System.out.println(colId);
            if(collectionsMapper.delete_CollectionsById(colId,userId)){
                return 0;  //删除成功
            }
            else {
                System.out.println("删除失败");
                return 1;
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return 3;  //数据库错误
        }
    }
    @Override
    public List<Collections> findCollectionByStatus(Integer status){
        List<Collections> collections = null;
        try{
            collections = collectionsMapper.select_CollectionsByStatus(status);
            return collections;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return collections;
        }
    }
    @Override
    public Collections findCollectionByColId(Integer colId){
        Collections collection=null;
        try{
            collection = collectionsMapper.select_CollectionsByColId(colId);
            return collection;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return collection;
        }
    }

}
